package com.dominator.serviceImpl;


import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.*;
import com.dominator.repository.DaoAccount;
import com.dominator.repository.DaoPointTicket;
import com.dominator.repository.DaoUser;
import com.dominator.service.ApiAccountService;
import com.dominator.service.ApiPointTicketService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApiPointTicketServiceImpl implements ApiPointTicketService {

    @Autowired
    DaoPointTicket pointTicketDao;
    @Autowired
    DaoUser userDao;
    @Autowired
    T_accountDao t_accountDao;
    @Autowired
    DaoAccount accountDao;

    @Autowired
    T_point_ticketDao t_point_ticketDao;


    @Autowired
    ApiAccountService apiAccountService;

    @Override
    public ApiMessage batchCreate(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"count,ticketName,platformId,points");
        if (dto.getInteger("count")<=0){
            throw new ApiException(ReqEnums.WRONG_NUM);
        }
        if (dto.getBigDecimal("points").compareTo(BigDecimal.ZERO)<=0){
            throw new ApiException(ReqEnums.WRONG_POINT);
        }
        List<Dto> list =new ArrayList<>();
        for (int i=0;i<dto.getInteger("count");i++){
            Dto dto1=Dtos.newDto();
            dto1.put("id", SystemIdUtils.uuid());
            dto1.put("ticket_name",dto.getString("ticketName"));
            dto1.put("platform_id",dto.getString("platformId"));
            dto1.put("points",dto.getBigDecimal("points"));
            dto1.put("exchange_code",SystemIdUtils.uuid());
            list.add(dto1);
        }
        pointTicketDao.batchInsert(list);
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage verification(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"id,promoteId");
        T_point_ticketPO t_point_ticketPO= t_point_ticketDao.selectByKey(dto.getString("id"));
        if (SystemUtils.isEmpty(t_point_ticketPO)){
            throw new ApiException(ReqEnums.TICKET_NOT_EXSIT);
        }else if ("1".equals(t_point_ticketPO.getStatus())){
            throw new ApiException(ReqEnums.TICKET_VERIFICATION);
        }
        t_point_ticketPO.setPromote_id(dto.getString("promoteId"));
        t_point_ticketPO.setModify_time(new Date());
        t_point_ticketPO.setStatus("1");
        t_point_ticketDao.updateByKey(t_point_ticketPO);
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage exchange(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum,exchangeCode");
        Dto qDto=Dtos.newDto();
        qDto.put("exchange_code",dto.getString("exchangeCode"));
        T_point_ticketPO t_point_ticketPO= t_point_ticketDao.selectOne(qDto);
        if (SystemUtils.isEmpty(t_point_ticketPO)){
            throw new ApiException(ReqEnums.TICKET_NOT_EXSIT);
        }else if ("1".equals(t_point_ticketPO.getStatus())){
            throw new ApiException(ReqEnums.TICKET_VERIFICATION);
        }

        //核销
        t_point_ticketPO.setModify_time(new Date());
        t_point_ticketPO.setStatus("1");
        t_point_ticketDao.updateByKey(t_point_ticketPO);

       //添加积分
        dto.put("points",t_point_ticketPO.getPoints());
        apiAccountService.addPoints(dto);


        return ApiMessageUtil.success();

    }

    @Override
    public ApiMessage getPointTicketList(Dto dto) throws ApiException {
        return null;
    }
}
